package com.geyue.controller;

import com.geyue.common.toolbox.result.Res;
import com.geyue.core.model.Addr;
import com.geyue.core.model.Business;
import com.geyue.core.model.User;
import com.geyue.core.service.AddrService;
import com.geyue.core.service.BusinessService;
import com.geyue.core.service.CityService;
import com.geyue.core.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

/**
 * Created by Lyon on 2017/5/11.
 */
@Controller
@RequestMapping("/business")
public class BusinessController {

    @Autowired
    private BusinessService businessService;
    @Autowired
    private ProductService productService;
    @Autowired
    private CityService cityService;
    @Autowired
    private AddrService addrService;

    @RequestMapping(value = "/", method = RequestMethod.GET)
    public String home(HttpSession session,Model model) {
        User userInfo = (User) session.getAttribute("user");
        //如果未登录或session过期
        if (userInfo == null)
            return "login";

        model.addAttribute("products",productService.getList());
        model.addAttribute("citys",cityService.getList());
        return "business";
    }

    @RequestMapping(value = "/", method = RequestMethod.POST)
    @ResponseBody
    public Res business(@Validated Business business, BindingResult binding, HttpSession session) {

        User userInfo = (User) session.getAttribute("user");
        //如果未登录或session过期
        if (userInfo == null)
            return Res.fail("用户信息过期,请重新登录!");

        if (binding.hasErrors())
            return Res.fail("请求参数错误!");

        business.setUid(userInfo.getId());
        return businessService.submit(business);
    }

    @RequestMapping(value = "/{uid}",method = RequestMethod.GET)
    public String list(@PathVariable Integer uid, Model model,HttpSession session) {
        User userInfo = (User) session.getAttribute("user");
        //如果未登录或session过期
        if (userInfo == null)
            return "login";

        model.addAttribute("data",businessService.getListByUid(uid));
        return "business_list";
    }

    @RequestMapping(value = "/{quarter}",method = RequestMethod.POST)
    @ResponseBody
    public Res quarter(@PathVariable Integer quarter,HttpSession session) {
        User userInfo = (User)session.getAttribute("user");
        //如果未登录或session过期
        if(userInfo == null)
            return Res.fail("用户信息过期,请重新登录!");

        return businessService.getTotal(quarter,userInfo.getId());
    }


    @RequestMapping(value = "/info/{id}",method = RequestMethod.GET)
    public String info(@PathVariable Integer id,HttpSession session,Model model) {
        User userInfo = (User) session.getAttribute("user");
        //如果未登录或session过期
        if (userInfo == null)
            return "login";

        model.addAttribute("products",productService.getList());
        model.addAttribute("citys",cityService.getList());
        model.addAttribute("data",businessService.getOneByKey(id));
        return "business_no";
    }

    @RequestMapping(value = "/", method = RequestMethod.PUT)
    @ResponseBody
    public Res edit(@Validated Business business, BindingResult binding, HttpSession session) {

        User userInfo = (User) session.getAttribute("user");
        //如果未登录或session过期
        if (userInfo == null)
            return Res.fail("用户信息过期,请重新登录!");

        if (binding.hasErrors())
            return Res.fail("请求参数错误!");

        business.setIsOk(Business.submit);
        business.setBecause("");
        if(businessService.updateNotEmpty(business) > 0)
            return Res.success("修改成功!");

        return Res.fail("修改失败!");
    }


    @RequestMapping(value = "/addr/{cid}",method = RequestMethod.POST)
    @ResponseBody
    public Res addr(@PathVariable Integer cid,HttpSession session) {
        User userInfo = (User)session.getAttribute("user");
        //如果未登录或session过期
        if(userInfo == null)
            return Res.fail("用户信息过期,请重新登录!");

        Addr addr = new Addr();
        addr.setCityId(cid);
        return Res.success("",addrService.getList(addr));
    }

}
